package bom.example.common.filter;


import bom.example.common.session.SessionProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author X.I.O
 * @title: GlobalInterceptor
 * @projectName demo
 * @description: TODO
 * @date 2021/11/12 10:25
 */
@Component
@Order(1)
@Slf4j
public class GlobalInterceptor extends HandlerInterceptorAdapter {


    private final transient ReentrantLock lock = new ReentrantLock();

    @Autowired
    private SessionProvider sessionProvider;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            sessionProvider.setAttribute(request, "lastOperateTime", Calendar.getInstance().getTime());
            System.out.println("全局拦截1");
            log.info("这是全局拦截1=======>"+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return true;
    }
}
